package com.paomi.client.activity.pay.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.person.model.CardJsonBean;
import com.paomi.client.utils.Util;
import com.paomi.client.widget.RxToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCardListAdapter extends RecyclerView.Adapter<MyCardListAdapter.DefaultViewHolder> {

    private Activity context;
    private Typeface mTypeface;
    private List<CardJsonBean> data = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private float sunNum;//票价总额

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public MyCardListAdapter(Activity context, Typeface typeface,float sunNum) {
        this.context = context;
        this.mTypeface = typeface;
        this.sunNum = sunNum;
    }

    public void setData(List<CardJsonBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @Override
    public DefaultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_my_card, parent, false);
        return new DefaultViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DefaultViewHolder holder, int position) {
        CardJsonBean bean = data.get(position);
        int width = Util.getDisplay(context).widthPixels - Util.dip2px(context, 30);
        Util.setWidthAndHeight(holder.rl_ls, (int) (width * 0.27), -1);
        Util.setWidthAndHeight(holder.rl_lx, (int) (width * 0.27), -1);
        Util.setWidthAndHeight(holder.rl_rs, (int) (width * 0.73), -1);
        Util.setWidthAndHeight(holder.rl_rx, (int) (width * 0.73), -1);

        holder.check_tv.setVisibility(View.GONE);
        holder.next_enter.setVisibility(View.VISIBLE);

        if (bean.getType() == 1) {
            GlideApp.with(context).asBitmap().load(R.mipmap.img_card_you_y).into(holder.iv_img);
        } else {
            GlideApp.with(context).asBitmap().load(R.mipmap.img_card_piao_y).into(holder.iv_img);
        }
        holder.tv_card_name.setText(context.getResources().getString(R.string.card_name) + bean.getName());
        holder.tv_msg.setText("满" + bean.getEnableAmount() + "使用");
        holder.tv_msg.setTextColor(context.getResources().getColor(R.color.color_fff3b4));
        if( bean.getAssName()!=null){
            holder.tv_card_assname.setText(context.getResources().getString(R.string.card_assName) + bean.getAssName());
        }
        holder.tv_card_data.setText(context.getResources().getString(R.string.card_data)
                + new SimpleDateFormat("yyyy-MM-dd").format(bean.getExpireDdate() * 1000));
        holder.next_enter.setTypeface(mTypeface);
        if (bean.getSelect() == 0) {
            holder.next_enter.setText(context.getResources().getString(R.string.uncheck_sex));
            holder.next_enter.setTextColor(context.getResources().getColor(R.color.text_gray_99));
        } else if (bean.getSelect() == 1) {
            holder.next_enter.setText(context.getResources().getString(R.string.check_sex));
            holder.next_enter.setTextColor(context.getResources().getColor(R.color.main_color));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sunNum >= bean.getEnableAmount()){
                    for (int i = 0; i < data.size(); i++) {
                        if (i == position) {
                            if(data.get(i).getSelect()==1){
                                data.get(i).setSelect(0);
                            }else {
                                data.get(i).setSelect(1);
                            }
                        } else {
                            data.get(i).setSelect(0);
                        }
                        notifyDataSetChanged();
                    }
                }else{
                    RxToast.info("满" + bean.getEnableAmount() + "使用");
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class DefaultViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_ls)
        RelativeLayout rl_ls;
        @BindView(R.id.rl_lx)
        RelativeLayout rl_lx;
        @BindView(R.id.rl_rs)
        LinearLayout rl_rs;
        @BindView(R.id.rl_rx)
        RelativeLayout rl_rx;
        @BindView(R.id.click)
        TextView check_tv;
        @BindView(R.id.tv_card_name)
        TextView tv_card_name;
        @BindView(R.id.tv_msg)
        TextView tv_msg;
        @BindView(R.id.tv_card_data)
        TextView tv_card_data;
        @BindView(R.id.tv_card_assname)
        TextView tv_card_assname;
        @BindView(R.id.next_enter)
        TextView next_enter;
        @BindView(R.id.iv_img)
        ImageView iv_img;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
//        void setOnItemClick(int pos);
    }
}
